﻿using System;
using System.Windows.Input;

namespace HB.MVVM
{
    class SimpleCommand : ICommand
    {
        Action Exec_Action;
        Func<bool> Exec_Func;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="A"></param>
        /// <param name="F">CanExecute</param>
        public SimpleCommand(Action A, Func<bool> F = null)
        {
            Exec_Action = A ?? throw new Exception("Action Can Not Be Null"); Exec_Func = F;
        }

        public event EventHandler CanExecuteChanged;

        public bool CanExecute(object parameter)
        {
            if (Exec_Func == null) { return true; } else { return Exec_Func.Invoke(); }
        }
        public void Execute(object parameter)
        {
            if (CanExecute(parameter))
            {
                Exec_Action.Invoke();
            }
        }
    }

    class SimpleCommand<T> : ICommand
    {
        public event EventHandler CanExecuteChanged;
        Action<T> Exec_Action;
        Func<T, bool> Exec_Func;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="A"></param>
        /// <param name="F">CanExecute</param>
        public SimpleCommand(Action<T> A, Func<T, bool> F = null) { Exec_Action = A ?? throw new Exception("Action Can Not Be Null"); ; Exec_Func = F; }

        public bool CanExecute(object parameter)
        {
            if (Exec_Func == null) { return true; }
            else
            {
                var p = (T)parameter;
                return Exec_Func.Invoke(p);
            }
        }

        public void Execute(object parameter)
        {
            T p = (T)parameter;
            if (CanExecute(p))
            {
                Exec_Action.Invoke(p);
            }
        }
    }
}
